#!/usr/bin/python
#This creates a header file, config.h, with configuration information
#== Start of code ===========================================================

import os
import sys

#Import environment
Import('SagittariusEnv')

def ConfigureHeaderCallback(target = None, source = None, env = None):
    #Don't know which targets go with which sources
    if len(target) != 1:
        raise RuntimeError, 'ConfigureHeaderCallback: More than one target'
    targetName = map(lambda x: str(x), target)[0]
    
    fileString = CreateConfigureString(env)
    
    trgFile = open( targetName, "w" )
    trgFile.write( fileString )
    trgFile.close()
    
    return 0

def CreateConfigureString(env):
    fileString = ""
    fileString += "//== SAGITTARIUS ============================================================================\n"\
    "\n"\
    "//== BEGINNING OF CODE ===============================================================\n\n"\
    "#ifndef __SAGITTARIUS_CONFIGURE_HEADER__\n"\
    "#define __SAGITTARIUS_CONFIGURE_HEADER__\n\n"
    
    fileString += "#define SAGITTARIUS_BIN_PATH \"" + SagittariusEnv['BIN_DIR'] + "\"\n\n"
    
    if( env['SAGITTARIUS_LOG_DIR'] ):
        fileString += "#define SAGITTARIUS_LOG_DIR \"" + env['SAGITTARIUS_LOG_DIR'] + "\"\n\n"
    else:
        fileString += "//#define SAGITTARIUS_LOG_DIR ?\n\n" #comment
    
    if( env['SAGITTARIUS_DATA_DIR'] ):
        fileString += "#define SAGITTARIUS_DATA_DIR \"" + env['SAGITTARIUS_DATA_DIR'] + "\"\n\n"
    else:
        fileString += "//#define SAGITTARIUS_DATA_DIR ?\n\n" #comment
    
    fileString += "#endif //#ifndef __SAGITTARIUS_CONFIGURE_HEADER__\n" #closing #if block for whole header
    
    return fileString

def BuildConfigureHeader(env, name):
    intermediate = env.ConfigureHeader(target = name+'.intermediate', source = None)
    AlwaysBuild(intermediate)
    return Command(name+'.hpp', intermediate, [Copy("$TARGET", "$SOURCE"), Chmod("$TARGET", 0755)])

ConfigureHeaderBuilder = Builder(action = ConfigureHeaderCallback)
SagittariusEnv.Append(BUILDERS = {'ConfigureHeader' : ConfigureHeaderBuilder})

ConfigureHeader = BuildConfigureHeader(SagittariusEnv, "config")
#if configure header doesn't change, don't rebuild
#AlwaysBuild(ConfigureHeader)

Return("ConfigureHeader")
